<template>
  <div class="common-layout">
    <el-container>
      <el-header class="header-out">
        <div class="header-in">
          <h3>奖助学金评定管理系统</h3>
          <span class="welcome">欢迎登录本系统</span>
        </div>
      </el-header>
      <el-main>
        <div class="login-out">
          <el-card class="box-card">
            <el-tabs v-model="activeName" class="demo-tabs" :stretch="true" type="border-card">
              <el-tab-pane label="登录" name="login">
                <el-form class="login-form">
                  <el-form-item label="账号">
                    <el-input placeholder="请输入账号" v-model="loginData.account"></el-input>
                  </el-form-item>
                  <el-form-item label="密码">
                    <el-input placeholder="请输入密码" showPassword v-model="loginData.password"></el-input>
                  </el-form-item>
                  <div class="footer-wrapped">
                    <div class="forget-password">
                      <span class="forget-password-button" @click="openForgetPassword">忘记密码</span>
                    </div>
                    <div class="footer-button">
                      <el-button type="primary" @click="Login">登录</el-button>
                    </div>
                    <div class="footer-go-register">
                      <span>没有账号?</span>
                      <span class="go-register">点击注册</span>
                    </div>
                  </div>
                </el-form>
              </el-tab-pane>
              <el-tab-pane label="注册" name="register">
                <el-form class="register-form">
                  <el-form-item label="账号">
                    <el-input placeholder="账号长度为6-12位" v-model="registerData.account"></el-input>
                  </el-form-item>
                  <el-form-item label="密码">
                    <el-input placeholder="密码长度为6-12位,包含字母和数字" type="password"
                      v-model="registerData.password"></el-input>
                  </el-form-item>
                  <el-form-item label="确认密码">
                    <el-input placeholder="请再次输入密码" type="password" v-model="registerData.rePassword"></el-input>
                  </el-form-item>
                  <div class="footer-button">
                    <el-button type="primary" @click="Register">注册</el-button>
                  </div>
                </el-form>
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </div>
      </el-main>
      <el-footer class="footer-out">
        <div class="footer-in">
          <span>2025/03/07</span>&nbsp;|&nbsp;
          <span>奖助学金评定管理系统</span>&nbsp;|&nbsp;
          <span>Powered by 龚道亮</span>
        </div>
      </el-footer>
    </el-container>
  </div>
  <forget ref="forgetP"></forget>
</template>

<script setup lang="ts">

import { reactive, ref } from 'vue';
import forget from './component/forget_password.vue'
import { register, login } from '@/api/login.js'
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import { useUserInfoStore } from '@/store/userInfo.js';
import { loginLog } from '@/api/log.js'
//创建pinia实例
const userStore = useUserInfoStore();
//创建路由实例
const router = useRouter();
//控制卡片默认显示
const activeName = ref('login');

//表单接口
interface formData {
  account: number;
  password: string;
  rePassword?: string;
}

//登陆数据
const loginData: formData = reactive({
  account: null,
  password: ''
})
//注册数据
const registerData: formData = reactive({
  account: null,
  password: '',
  rePassword: ''
})
const forgetP = ref();
//打开忘记密码
const openForgetPassword = () => {
  forgetP.value.open();
}
//注册方法
const Register = async () => {
  if (registerData.password === registerData.rePassword) {
    const res = await register(registerData);
    console.log(res)
    if (res.data.message === '注册成功!') {
      ElMessage.success('账号注册成功');
      activeName.value = 'login';
    } else {
      ElMessage.error('账号注册失败');
    }
  } else {
    ElMessage.error('两次密码不一致');
  }
}
//登录方法
const Login = async () => {
  if (loginData.password.length >= 6) {
    const res = await login(loginData);
    const { token } = res;
    //res.data.results.status 确保没有被冻结
    if (res.data.status === 0 && res.data.results.status === '0') {
      console.log(res.data.results);
      const { ID } = res.data.results;
      ElMessage.success('登录成功');
      localStorage.setItem('token', token);
      userStore.fetchUserInfo(ID)
      //登陆的时候保存登录日志
      await loginLog(userStore.account, userStore.name, userStore.email)
      //跳转到首页
      router.push('/home');
    } else {
      ElMessage.error('登陆失败,账号或密码错误');
    }
  } else {
    ElMessage.error('密码长度不正确');
  }
}
</script>


<style scoped lang="scss">
.header-out {
  .header-in {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .welcome {
      font-size: 13px;
      font-weight: bold;
    }
  }
}

.el-main {
  background-image: url("@/assets/loginPage.jpg");
  background-size: cover;
  height: 600px;
  --el-main-padding: 0;

  .login-out {
    width: 1200px;
    height: 600px;
    margin: 0 auto;

    .box-card {
      width: 350px;
      height: 375px;
      float: right;
      position: relative;
      top: 14%;

      .login-form {
        .footer-wrapped {
          display: flex;
          flex-direction: column;

          .forget-password {
            display: flex;
            justify-content: flex-end;

            //margin-top: 10px ;
            .forget-password-button {
              font-size: 12px;
              color: #409eff;
              cursor: pointer;
              margin-bottom: 10px;
            }
          }


          .footer-go-register {
            font-size: 12px;
            margin: 12px 0;
            display: flex;
            justify-content: center;

            .go-register {
              font-size: 12px;
              color: #409eff;
              cursor: pointer;
            }
          }
        }
      }

      .footer-button {
        width: 100%;
        display: flex;
        justify-content: center;
      }
    }
  }
}

.footer-out {
  margin-top: 10px;

  .footer-in {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;

    span {
      font-size: 13px;
      color: #666;
    }
  }
}

.el-form {
  margin-top: 30px;

  .el-form-item {
    margin-bottom: 20px;
  }
}

//tabs
:deep(.el-tabs__item) {
  color: #333;
  font-size: 18px;
}

//输入框高度
:deep(.el-input__inner) {
  height: 40px;
}

//输入框标签字体高度
:deep(.el-form-item__label) {
  height: 40px;
  line-height: 40px;
}

:deep(.el-button) {
  width: 300px;
  height: 45px;
  font-size: 16px;
}
</style>